import 'package:cached_network_image/cached_network_image.dart';
import 'package:flutter/material.dart';
import 'package:gzmall_app/common/api.dart';
import 'package:gzmall_app/common/color_common.dart';
import 'package:gzmall_app/common/net_util.dart';
import 'package:gzmall_app/goods/goods_details_page.dart';
import 'package:gzmall_app/model/order_item.dart';
import 'package:gzmall_app/mine/order_comment_page.dart';

class OrderProductCommentPage extends StatefulWidget {
  int orderId;

  OrderProductCommentPage(this.orderId);

  @override
  State<StatefulWidget> createState() =>
      OrderProductCommentPageState(this.orderId);
}

class OrderProductCommentPageState extends State<OrderProductCommentPage> {
  int orderId;

  List<OrderItem> _items = [];

  OrderProductCommentPageState(this.orderId);

  @override
  void initState() {
    super.initState();
    _getOrdersEvaluate(orderId);
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: _createAppBar(),
      body: Container(
        color: ColorCommon.grey5,
        child: ListView.builder(
          itemCount: _items.length,
          itemBuilder: (context, index) {
            OrderItem item = _items[index];
            return item != null
                ? InkWell(
                    onTap: () {
                      Navigator.of(context)
                          .push(new MaterialPageRoute(builder: (_) {
                        return new GoodsDetailsPage(productId: item.spuId);
                      }));
                    },
                    child: Container(
                        color: Colors.white,
                        padding: const EdgeInsets.all(10.0),
                        margin: const EdgeInsets.only(bottom: 4.0),
                        child: Row(
                          children: <Widget>[
                            item.avatar != null
                                ? CachedNetworkImage(
                                    width: 80.0,
                                    height: 80.0,
                                    imageUrl: item.avatar,
                                    fit: BoxFit.fill,
                                  )
                                : Container(
                                    width: 80.0,
                                    height: 80.0,
                                    color: ColorCommon.grey2,
                                  ),
                            Expanded(
                              child: Container(
                                margin: const EdgeInsets.only(left: 10.0),
                                child: Column(
                                  crossAxisAlignment: CrossAxisAlignment.start,
                                  children: <Widget>[
                                    Container(
                                      height: 50.0,
                                      child: Text(
                                        item.name ?? "",
                                        style: TextStyle(
                                            color: ColorCommon.black1,
                                            fontSize: 14.0),
                                        maxLines: 2,
                                        overflow: TextOverflow.ellipsis,
                                      ),
                                    ),
                                    InkWell(
                                      onTap: () {
                                        Navigator.of(context).push(
                                            MaterialPageRoute(builder: (_) {
                                          return OrderCommentPage(item);
                                        })).then((r) {
                                          if (r) {
                                            _getOrdersEvaluate(orderId);
                                          }
                                        });
                                      },
                                      child: Align(
                                        alignment: Alignment.centerRight,
                                        child: _getTagView("评价商品", Colors.white,
                                            ColorCommon.primaryColor, ColorCommon.primaryColor),
                                      ),
                                    ),
                                  ],
                                ),
                              ),
                            ),
                          ],
                        )))
                : Container();
          },
        ),
      ),
    );
  }

  _createAppBar() {
    return new AppBar(
      elevation: 0.0,
      title: new Text("订单评价"),
      leading: new IconButton(
        icon: new Icon(Icons.arrow_back_ios),
        onPressed: () {
          Navigator.of(context).pop();
        },
      ),
      actions: <Widget>[],
    );
  }

  Widget _getTagView(
      String text, Color bgColor, Color borderColor, Color textColor,
      {DecorationImage image}) {
    BoxDecoration decoration;
    if (image == null) {
      decoration = BoxDecoration(
          color: bgColor,
          border: Border.all(color: borderColor),
          borderRadius: BorderRadius.all(Radius.circular(100.0)));
    } else {
      decoration = BoxDecoration(image: image);
    }
    return Container(
      padding: const EdgeInsets.only(left: 8.0, right: 8.0),
      decoration: decoration,
      child: Text(
        text,
        style: TextStyle(fontSize: 13.0, color: textColor),
      ),
    );
  }

  _getOrdersEvaluate(int id) {
    NetUtil.get(context, Api.orders + "/" + id.toString() + "/evaluate",
        (data, message) {
      if (!mounted) return;
      setState(() {
        _items = (data as List)
            ?.map((e) => e == null ? null : OrderItem.fromJson(e))
            ?.toList();
      });
    });
  }
}
